<?php

namespace App\Providers;

use App\Models\Carousel;
use App\Models\Cart;
use App\Models\Classify;
use App\Models\Collect;
use App\Models\Goods;
use App\Observers\ClassifyObserver;
use App\Observers\GoodsObserver;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\View\View;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
     //注册观察表
        Goods::observe(GoodsObserver::class);
        Classify::observe(ClassifyObserver::class);
        Schema::defaultStringLength(191);
        $this->getClassify();
    }
//获得所有的分类数据,循环到主题市场中
    public function getClassify(){
       
       view()->composer('home/*',function (View $view){
           
           $classifys=Classify::where('pid',0)->get();
           if (auth()->check()){
               $carts=Cart::where('user_id',auth()->user()->id)->get();
               //获取该登录用户收藏的商品,获取id
               $wishId=Collect::where('user_id',auth()->id())->pluck('goods_id')->toArray();
           }
           $view->with(compact('classifys','carts','wishId'));
           
       });
    }
    public function register()
    {
        if ($this->app->environment() !== 'production') {
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
        }
    }
}
